package com.calm.logincenter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Create by @Calm
 *
 * @date 2021/3/5 14:22
 * @note
 **/
public class LoginActivity extends AppCompatActivity {
    private EditText etName,etPass;
    private Bundle mBundle;
    private String jumpClass;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        jumpClass = getIntent().getStringExtra("jumpClass");
        mBundle = getIntent().getExtras();
        etName = findViewById(R.id.login_name);
        etPass = findViewById(R.id.login_pass);
    }

    public void loginAccount(View view) {
        String name = etName.getText().toString();
        if(TextUtils.isEmpty(name)){
            Toast.makeText(this,"用户名不能为空!",Toast.LENGTH_SHORT).show();
            return;
        }
        String pass = etPass.getText().toString();
        if(TextUtils.isEmpty(pass)){
            Toast.makeText(this,"用户密码不能为空!",Toast.LENGTH_SHORT).show();
            return;
        }
        if(name.equals("Calm") && pass.equals("123456")){
            SharedPreferences sharedPreferences = getSharedPreferences("CALM", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("login",true);
            editor.apply();
            Toast.makeText(this,"登录成功!",Toast.LENGTH_SHORT).show();
            if(!TextUtils.isEmpty(jumpClass)){
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName(this,jumpClass);
                intent.setComponent(componentName);
                if(mBundle != null){
                    intent.putExtras(mBundle);
                }
                startActivity(intent);
            }
            finish();
        }else {
            Toast.makeText(this,"登录失败!",Toast.LENGTH_SHORT).show();
        }
    }
}
